<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>仅仅反转字母</title>
</head>
<body>
  <script>
    function fn(S) {
      let res = /[a-zA-Z]/
      // 存储需要逆转的数组
      let arr = []
      // 原数组
      let sum = S.split('')
      for (let i = 0; i < sum.length; i++) {
        if (res.test(sum[i])) {
          arr.push(sum[i])
        }
      }
      // 替换插入 
      for (let i = 0; i < sum.length; i++) {
        if (res.test(sum[i])) {
          sum[i] = arr.pop()
        }
      }
      return sum.join('')
    }
    console.log(fn('ab-cd'))
  </script>
</body>
</html>